package com.company.project.learning.zaluan;

/**
 * @Author: zhaoqi
 * @Date: 11/13/2019 10:54 AM
 * @Version 1.0
 */

public class sort1 {

    public static void main(String[] args) {
        int[] array = {6,98,5,54,2};
        int[] s = sort1.bubbleSort(array);
        for (int i = 0; i < s.length; i++) {
            System.out.println(s[i]);
        }
    }


    /**
     * 冒泡排序
     * 循环比较相邻的两个数，将较大的数放在后面
     * @param nums 待排序数值序列
     */
    private static int[] bubbleSort(int[] nums) {
        int len = nums.length;
        if(len == 0 || len == 1) {
            return nums;
        }
        for(int i = 0; i < len; i++) {
            for(int j = 0, subLen = len - 1 - i; j < subLen; j++) {
                if(nums[j + 1] < nums[j]) {
                    int tmp = nums[j + 1];
                    nums[j + 1] = nums[j];
                    nums[j] = tmp;
                }
            }
        }
        return nums;
    }
}
